package com.phone.handle;

import com.phone.entity.ServiceControl;
import com.phone.entity.ServiceRecord;
import com.phone.exception.NoBindingIdException;
import org.hibernate.Session;

import java.util.List;

/**
 * Created by tisen on 2017/8/28.
 */
public class ControlHandle {
    private Session session;

    public ControlHandle(Session session) {
        this.session = session;
    }

    public Session getSession() {
        return session;
    }

    public void setSession(Session session) {
        this.session = session;
    }

    public void save(String model, boolean status) throws Exception {
        ServiceControl serviceRecord = new ServiceControl(model, status);
        session.saveOrUpdate(serviceRecord);
    }

    public ServiceControl get(String model) throws Exception {
        ServiceControl serviceRecord = session.get(ServiceControl.class, model);
//        if (serviceRecord == null)
//            throw new NoBindingIdException();
        return serviceRecord;
    }

    public List<ServiceControl> search(String model, boolean isVague) {
        if (isVague) {
            return session.createQuery("from ServiceControl  where model like '%" + model + "%'").list();
        } else {
            return session.createQuery("from  ServiceControl where model = '" + model + "'").list();
        }
    }

    public List<ServiceControl> search() {
        return session.createQuery("from  ServiceControl ").list();
    }
}
